let app = getApp()
Page({
  data: {
    currentTab: 0,
    changeSelect: 0,
    changeSearch: 0,
    showSelectStatus: false,
    list: [],
    nodata: 'none',
    accounts: ["请选择"],
    accountIndex: 0,
    modalComfirm: false,
    scrollTop: 0,
    hide: "block",
    loading: 1,
    loadDealing: 0, //判断是否 由 待处理变成处理中
    catchMove: true,
    /**
     * 页面配置
     */
    winWidth: 0,
    winHeight: 0,
    // tab切换
    identityId: null,
    tabTitle: "待处理",
    pageTimes: 1,
    noMore: 0,
    indicatorDots: false,
    autoplay: false,
    interval: 3000,
    authCode: '',
    isSupervsor: false,
    isSupervising: false,
    isSuddenSupervising: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    console.log('relunch')
    if (app.globalData.departmentType == "教育局" || app.globalData.departmentType == "区教育局") {
      this.setData({
        isSupervsor: true,
        isSupervising: app.globalData.isSupervising,
        isSuddenSupervising: app.globalData.isSuddenSupervising
      })
    }

    let identityTrue = false
    if (app.globalData.features == "35" || app.globalData.identityId == -1 || app.globalData.identityId == -2) {
      identityTrue = true
    }
    this.setData({
      identityId: app.globalData.identityId,
      showForward: identityTrue ? 0 : 1
    })
    if (identityTrue) {
      this.setData({
        tabTitle: "我的上报"
      })
    }
    dd.getSystemInfo({
      success: res => {
        this.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

    //初始化 待处理隐患
    this.getSafetySenceList(6)
    dd.httpRequest({
      url: app.baseUrl + '/DingDingSystem/getAllValidInspectItems',
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      data: JSON.stringify({
        schoolId: app.globalData.departmentId
      }),
      dataType: 'json',
      success: res => {
        let accounts = [],
          categoryId = []
        for (let i = 0; i < res.data.length; i++) {
          accounts.push({
            item: res.data[i].category,
            realname: res.data[i].realname,
            checked: false
          })
          categoryId.push(res.data[i].categoryId)
        }
        this.setData({
          accounts: accounts,
          categoryId: categoryId,
        })
      }
    })
  },

  onShow() {
    if (this.data.loadDealing == 1) {
      this.setData({
        currentTab: 1,
        loadDealing: 0,
        loading: 0,
        pageTimes: 1
      })
      this.getSafetySenceList(7)
    }
    if (app.globalData.departmentType == "教育局" || app.globalData.departmentType == "区教育局") {
      this.setData({
        isSupervising: app.globalData.isSupervising,
        isSuddenSupervising: app.globalData.isSuddenSupervising
      })
    }
  },
  //处理中，已处理，待处理选择
  scrolltolower() {
    if (this.data.noMore == 0 && this.data.wait == 0) {
      this.setData({
        catchMove: false
      })
      if (this.data.currentTab == 0) {
        this.getSafetySenceList(6)
      } else if (this.data.currentTab == 1) {
        this.getSafetySenceList(7)
      } else {
        this.getSafetySenceList(5)
      }
    }
  },

  //获取安全现场数据集合
  getSafetySenceList(status) {
    this.setData({
      wait: 1
    })
    let interfaceUrl
    let identityTrue = false
    if (app.globalData.features == "35" || app.globalData.identityId == -1 || app.globalData.identityId == -2) {
      identityTrue = true
    }
    if (identityTrue) {
      interfaceUrl = app.baseUrl + '/DingDingSystem/listMyReportSafetyScene'
    } else {
      if (app.globalData.departmentType == "教育局" || app.globalData.departmentType == "区教育局") {
        interfaceUrl = app.baseUrl + '/DingDingSystem/getSafetySceneListForEdu'
      } else {
        interfaceUrl = app.baseUrl + '/DingDingSystem/getSafetySceneList'
      }

    }
    dd.httpRequest({
      url: interfaceUrl,
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      data: JSON.stringify({
        userId: app.globalData.userId, //用户Id
        schoolId: app.globalData.departmentId, //用户所属学校
        identityId: app.globalData.identityId, //用户身份Id
        status: status, //状态
        pageTimes: this.data.pageTimes
      }),
      dataType: 'json',
      success: res => {

        console.log(" app.globalData.userId================", app.globalData.userId)
        console.log("res.data===========================", res.data)
        if (res.data.data.length == 0) {
          this.setData({
            loading: 0,
            nodata: 'flex'
          })
          return
        } else {
          this.setData({
            loading: 0,
            nodata: 'none'
          })
        }
        let list = []
        for (let i = 0; i < res.data.data.length; i++) {
          let photo = ""
          if (res.data.data[i].photo == "" || !res.data.data[i].photo) {
            photo = "/images/nophoto.png"
          } else {
            photo = app.imageUrl + res.data.data[i].photo.split(',')[0]
          }

          list.push({
            id: res.data.data[i].id,
            address: res.data.data[i].address,
            safetyLevel: res.data.data[i].safetyLevel,
            category: res.data.data[i].categoryName,
            photo: photo,
            reporter: res.data.data[i].reportName,
            time: res.data.data[i].createTime,
            status: res.data.data[i].status,
            content: res.data.data[i].content,
            modifyTime: res.data.data[i].modifyTime,
            //评价
            evaluate: res.data.data[i].reporter == app.globalData.userId ? 1 : 0
            // userPush:res.data.data[i].userPush    已推送 负责人
          })

        }

        // console.log(list)
        let listRes
        // (this.data.list.concat(list)))
        if (this.data.change == 1) {
          listRes = list
        } else {
          let ids = list.map(item => item.id)
          listRes = list
          for (let i = 0; i < list.length; i++) {
            ids.indexOf(list[i].id) == -1 ? listRes.push(list[i]) : ''
          }
        }
        console.log(listRes)
        // if (status + this.data.currentTab == 6) {
        this.setData({
          list: listRes
        })
        if ("NO" == res.data.isEnd) {
          this.setData({
            noMore: 0,
            pageTimes: this.data.pageTimes + 1
          })
        } else {
          this.setData({
            noMore: 1
          })
        }
        // }


        // console.log(this.data.list)
      },
      fail: res => {
        dd.showToast({
          content: '网络异常！',
          type: 'fail'
        })
        this.setData({
          catchMove: true
        })
      },
      complete: () => {
        this.setData({
          wait: 0,
          catchMove: true
        })
        setTimeout(() => {
          this.setData({
            change: 0
          })
        }, 300)

      }
    })
  },


  dealnow(e) {
    console.log("s1:" + e.currentTarget.id)
    dd.navigateTo({
      url: '/page/safetysenceDeal/safetysenceDeal?id=' + e.currentTarget.id
    })
  },
  showHistory(e) {
    dd.navigateTo({
      url: '/page/safetysenceHistory/safetysenceHistory?id=' + e.currentTarget.id + `&evaluate=` + e.currentTarget.dataset.evaluate
    })
  },
  /**
   * tab滑动
   */
  change(e) {
    this.setData({
      list: [],
      pageTimes: 1,
      loading: 1,
      noMore: 0,
      change: 1,
      catchMove: false,
      hide: "block",
      currentTab: e.detail.current
    })

    if (this.data.currentTab == 0) {
      this.getSafetySenceList(6)
    } else if (this.data.currentTab == 1) {
      this.getSafetySenceList(7)
    } else {
      this.getSafetySenceList(5)
    }
    this.setData({
      currentTab: e.detail.current
    })
  },
  /**
   * 点击tab切换
   */
  swichNav(e) {
    this.setData({
      list: [],
      pageTimes: 1,
      loading: 1,
      noMore: 0,
      change: 1,
      catchMove: false,
      hide: "block",
      currentTab: e.currentTarget.dataset.current
    })
    if (this.data.currentTab == 0) {
      this.getSafetySenceList(6)
    } else if (this.data.currentTab == 1) {
      this.getSafetySenceList(7)
    } else {
      console.log("=======5")
      this.getSafetySenceList(5)
    }
  },

  report() {
    if (this.data.isSupervising) {
      dd.navigateTo({
        url: '/page/safetysenceReport/safetysenceReport?type=0&isSupervising=${this.data.isSupervising}'
      })
    } else {
      dd.navigateTo({
        url: '/page/safetysenceReport/safetysenceReport?type=0'
      })
    }


  },
  radioSelect(e) {
    console.log("--------------------:" + e)
    let radioIndex = e.currentTarget.dataset.index,
      accounts = this.data.accounts.map((item, index) => {
        return {
          item: item.item,
          checked: index == radioIndex ? true : false,
          realname: item.realname
        }
      }),
      status
    this.setData({
      accounts: accounts,
      selectedIndex: radioIndex
    })
    if (this.data.selectedIndex != null) status = true
    else status = false
    this.setData({
      modalComfirm: status
    })
  },
  confirmSelect(e) {
    this.setData({
      accountIndex: this.data.selectedIndex,
      modalName: null
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },


  handletouchstart: function (e) {
    console.log(e)
    this.setData({
      touchStart: e.changedTouches[0].clientY,
      touchStartSymbol: 1
    })
  },

  handletouchmove: function (e) {
    console.log(e)
    // console.log(e.changedTouches[0].clientY)
    // if(this.noMore==0) return
    if (this.data.touchStartSymbol == 1) {
      if (this.data.touchStart > e.changedTouches[0].clientY) {
        this.setData({
          hide: 'none',
          touchStartSymbol: 0,
          touchNow: e.changedTouches[0].clientY
        })
      } else {
        this.setData({
          hide: 'block',
          touchStartSymbol: 0,
          touchNow: e.changedTouches[0].clientY
        })
      }
    } else {
      if (this.data.touchNow > e.changedTouches[0].clientY) {
        this.setData({
          hide: 'none',
          touchStartSymbol: 0,
          touchNow: e.changedTouches[0].clientY
        })
      } else {
        this.setData({
          hide: 'block',
          touchStartSymbol: 0,
          touchNow: e.changedTouches[0].clientY
        })
      }
    }
  },

});